// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { BuildType, MapUnitType } from '../define/GameDefine';
import ItemGrid from '../items/ItemGrid';
import { MapUnit } from '../object/MapUnit';
import RandomTool from '../tool/RandomTool';
import { tool } from '../tool/UITool';

const { ccclass, property } = cc._decorator;

@ccclass
export default class mapUnitView extends cc.Component {
  // LIFE-CYCLE CALLBACKS:

  // onLoad () {}

  /**
   * 格子地图
   */
  @property(cc.Sprite)
  bg: cc.Sprite = null;

  @property(cc.Sprite)
  build: cc.Sprite = null;

  @property(cc.Sprite)
  itemShow: cc.Sprite = null;

  @property(cc.Node)
  hpbar: cc.Node = null;

  @property(cc.Node)
  hpMask: cc.Node = null;

  @property(cc.Label)
  testLabel: cc.Label = null;

  @property(ItemGrid)
  itemGrid: ItemGrid = null;

  start() {}

  public unit: MapUnit;
  public init(unit: MapUnit): void {
    this.unit = unit;
    this.renderGird();
  }

  onLoad() {
    this.itemGrid.node.scale = 0.7;
  }

  private renderGird(): void {
    this.build.node.active = false;
    this.itemShow.node.active = true;
    this.node.zIndex = 10;
    if (this.unit.unitType == MapUnitType.Nothing) {
      let ranMp: number = Math.ceil(Math.random() * 4);
      let bgSource: string = 'mapTile/md' + ranMp;
      tool.setSource(this.bg, bgSource);
      this.itemShow.node.active = false;
    } else if (this.unit.unitType < BuildType.Min) {
      //路障
      let img: string = this.getLockImg(this.unit.unitType);
      let bgSource: string = 'mapTile/' + img;
      tool.setSource(this.itemShow, bgSource);
      this.testLabel.string = `${this.unit.unitType}`;
      if (this.unit.unitType == MapUnitType.Tree) {
        this.node.zIndex = 50;
      }
      // @ts-ignore
    } else {
      this.itemShow.node.active = false;
      if (this.unit.buildCom == 1) {
        this.build.node.active = true;
        let buildState: string = this.unit.buildState.toString();
        let bgSource: string = 'mapTile/' + this.getBuild(this.unit.unitType) + buildState;
        tool.setSource(this.build, bgSource);
        this.node.zIndex = 30;
      } else {
        this.build.node.active = false;
      }
    }

    const len = this.unit.items.length;
    const item = this.unit.items[len - 1];
    this.itemGrid.setItem(item);
  }

  private getLockImg(id: MapUnitType): string {
    let ranshow = Math.ceil(RandomTool.getRandom() * 3);
    switch (id) {
      case MapUnitType.LuZhang:
        return 'luzhang' + ranshow;
        break;
      case MapUnitType.Stone:
        return 'stone' + ranshow;
        break;
      case MapUnitType.Tree:
        return 'tree' + ranshow;
        break;
    }
  }

  private getBuild(id: number): string {
    switch (id) {
      case BuildType.FrutShop:
        return 'fruit';
        break;
      case BuildType.Hospital:
        return 'hospital';
        break;
      case BuildType.EXRoom:
        return 'exroom';
        break;
      case BuildType.Shop_Eye:
        return 'shop';
        break;
      case BuildType.Shop_Clock:
        return 'shop';
        break;
      case BuildType.Shop_Mac:
        return 'shop';
        break;
      case BuildType.PostBox:
        return 'post';
        break;
      case BuildType.OfficeBuild:
        return 'office';
        break;
      case BuildType.Pauper:
        return 'pua';
        break;
    }
  }

  /**
   * 玩家交互
   */
  public onChange(): void {
    if (this.unit.unitType != MapUnitType.Nothing && this.unit.unitType < 5) {
      this.hpbar.active = true;
      this.renderHp();
      cc.tween(this.hpbar).stop();
      cc.tween(this.hpbar)
        .delay(1)
        .call(() => {
          this.hpbar.active = false;
        })
        .start();
    } else {
      this.hpbar.active = false;
      this.renderGird();
    }
  }

  /**
   * render
   */
  private renderHp(): void {
    let per: number = this.unit.hp / this.unit.maxHp;
    this.hpMask.width = 64 * per;
  }

  // update (dt) {}
}
